﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConnectFour
{
    class Moderator
    {

        Player playerOne, playerTwo, curPlayer;
        Board board;
        Checker winner;
        static Random random = new Random();

        public Checker Winner
        {
            get { return winner; }
            set { winner = value; }
        }

        public Board Board
        {
            get { return board; }
        }

        /// <summary>
        /// Creates a new moderator game with the specified players
        /// </summary>
        /// <param name="playerOneName">name for player one</param>
        /// <param name="playerTwoName">name for player two</param>
        public Moderator(String playerOneName, String playerTwoName)
        {
            board = new Board();

            setPlayer(playerOneName, ref playerOne, Checker.Red);
            setPlayer(playerTwoName, ref playerTwo, Checker.Black);

            curPlayer = random.Next(2) == 0 ? playerOne : playerTwo;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lastMove"></param>
        /// <param name="row"></param>
        /// <param name="column"></param>
        /// <param name="checkerColor"></param>
        /// <returns></returns>
        public bool NextMove(int lastMove, out int row, out int column, out Checker checkerColor)
        {
            checkerColor = curPlayer.Checker;
            column = curPlayer.Move(lastMove);
            row = board.Play(curPlayer.Checker, column);
            curPlayer = curPlayer == playerOne ? playerTwo : playerOne;
            return board.CheckForWinner(out winner);
        }

        /// <summary>
        /// Prints out the board's current state
        /// </summary>
        public void PrintBoard()
        {
            Console.Out.WriteLine(board.ToString());
        }

        private void setPlayer(String playerName, ref Player player, Checker color)
        {
            switch (playerName)
            {
                case "random":
                    player = new RandomPlayer(color, board);
                    break;
                case "human":
                    player = new HumanPlayer(color, board);
                    break;
                case "tony":
                case "jake":
                case "reid":
                case "miles":
                    player = new FileBasedPlayer(color, board, playerName);
                    break;
                case "tyler":
                    player = new TylerPlayer(color, board);
                    break;
                case "aaron":
                    player = new AaronPlayer(color, board);
                    break;
                case "ryan":
                    player = new Ryan(color, board);
                    break;
            }
        }

        public void GameOver()
        {
            playerOne.GameOver(Winner);
            playerTwo.GameOver(Winner);
        }

        /*static void Main(string[] args)
        {
            Moderator m = new Moderator("random", "random");

            int row, column = 0;
            Checker checker;

            while (!m.NextMove( column, out row, out column, out checker))
            {
                m.PrintBoard();
                Console.Out.WriteLine("{0} played at ({1}, {2})", checker, column, row);
            }
            m.PrintBoard();
            Console.Out.WriteLine("{0} played at ({1}, {2})", checker, column, row);
            if (m.Winner != Checker.Empty)
            {
                Console.Out.WriteLine("{0} won!", m.Winner);
            }
            else
            {
                Console.Out.WriteLine("Draw Game!");
            }
            m.GameOver();

            Console.In.Read();
        }*/
    }
}
